package com.zsh.console.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zsh.console.model.Pic;
import com.zsh.console.service.PicManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * Created by xinghao1 on 2017/7/17.
 */
@Service("picManager")
public class PicManagerImpl implements PicManager {

    private static Logger logger = LoggerFactory.getLogger(PicManagerImpl.class);

    private URI nextPicUri;
    private URI nocheatUri;
    private URI cheatUri;

    private SimpleClientHttpRequestFactory requestFactory;

    @PostConstruct
    public void init() throws URISyntaxException {
        nextPicUri = new URI("http://localhost:11112/process/manual/next_pic");
        nocheatUri = new URI("http://localhost:11112/process/manual/uncheat");
        cheatUri = new URI("http://localhost:11112/process/manual/cheat");
        requestFactory = new SimpleClientHttpRequestFactory();
    }


    @Override
    public Pic getPic() {
        try {
            ClientHttpRequest request = requestFactory.createRequest(nextPicUri, HttpMethod.GET);
            String httpResponse = getHttpResponse(request);
            return JSON.parseObject(httpResponse, Pic.class);
        } catch (IOException e) {
            logger.error("http请求失败", e);
        }
        return null;
    }

    private String getHttpResponse(ClientHttpRequest request) throws IOException {
        //chr.getBody().write(param.getBytes());//body中设置请求参数
        ClientHttpResponse res = request.execute();
        InputStream is = res.getBody(); //获得返回数据,注意这里是个流
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder builder = new StringBuilder();
        String str;
        while ((str = br.readLine()) != null) {
            builder.append(str);
        }
        return builder.toString();
    }

    @Override
    public List<Pic> search(String picId) {
        return getPics();
    }

    @Override
    public List<Pic> getPics() {
        Pic pic = getPic();
        if (null == pic) {
            return Lists.newArrayList(defaultPic());
        }
        return Lists.newArrayList(pic);
    }

    private Pic defaultPic() {
        Pic pic = new Pic();
        pic.setFullPath("https://www.flickr.com/photos/mcginityphoto/6716288117/");
        pic.setPicname("没有啦");
        return pic;
    }

    @Override
    public void trustAndDelete(String fullpath) {
        try {
            ClientHttpRequest request = requestFactory.createRequest(nocheatUri, HttpMethod.POST);
            request.getHeaders().add("current", fullpath);
            getHttpResponse(request);
        } catch (IOException e) {
            logger.error("http请求失败", e);
        }
    }

    @Override
    public void dealCheat(String fullpath) {
        try {
            ClientHttpRequest request = requestFactory.createRequest(cheatUri, HttpMethod.POST);
            request.getHeaders().add("current", fullpath);
            getHttpResponse(request);
        } catch (IOException e) {
            logger.error("http请求失败", e);
        }
    }
}
